<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */
defined('IN_IA') or exit('Access Denied');

load()->model('miniapp');
load()->classs('uploadedfile');
load()->func('file');

$dos = ['front_download', 'getpackage', 'set_appurl', 'upload_verify_txt_file', 'entrychoose', 'set_wxapp_entry', 'platform_version_manage',
		'custom', 'custom_save', 'custom_default', 'custom_convert_img', 'upgrade_module', 'tominiprogram', 'upload_private_key', 'rgapi_publish', 'rgapi_publish_status', 'publish_buy', 'auth_domain_list', 'auth_domain_post'];
$do = in_array($do, $dos) ? $do : 'front_download';

$wxapp_info = miniapp_fetch($_W['uniacid']);


$is_module_wxapp = false;
if (!empty($version_id)) {
	$is_single_module_wxapp = WXAPP_CREATE_MODULE == $version_info['type']; 
}




if ('custom' == $do) {
	$default_appjson = miniapp_code_current_appjson($version_id);
	$default_appjson = json_encode($default_appjson);
	template('wxapp/version-front-download');
}

if ('custom_default' == $do) {
	$result = miniapp_code_set_default_appjson($version_id);
	if (false === $result) {
		iajax(1, '操作失败，请重试！');
	} else {
		iajax(0, '设置成功！', url('wxapp/front-download/front_download', array('version_id' => $version_id)));
	}
}


if ('custom_save' == $do) {
	if (empty($version_info)) {
		iajax(1, '参数错误！');
	}
	$json = array();
	if (!empty($_GPC['json']['window'])) {
		$json['window'] = array(
			'navigationBarTitleText' => safe_gpc_string($_GPC['json']['window']['navigationBarTitleText']),
			'navigationBarTextStyle' => safe_gpc_string($_GPC['json']['window']['navigationBarTextStyle']),
			'navigationBarBackgroundColor' => safe_gpc_string($_GPC['json']['window']['navigationBarBackgroundColor']),
			'backgroundColor' => safe_gpc_string($_GPC['json']['window']['backgroundColor']),
		);
	}
	if (!empty($_GPC['json']['tabBar'])) {
		$json['tabBar'] = array(
			'color' => safe_gpc_string($_GPC['json']['tabBar']['color']),
			'selectedColor' => safe_gpc_string($_GPC['json']['tabBar']['selectedColor']),
			'backgroundColor' => safe_gpc_string($_GPC['json']['tabBar']['backgroundColor']),
			'borderStyle' => in_array($_GPC['json']['tabBar']['borderStyle'], array('black', 'white')) ? safe_gpc_string($_GPC['json']['tabBar']['borderStyle']) : '',
		);
	}
	$result = miniapp_code_save_appjson($version_id, $json);
	cache_delete(cache_system_key('miniapp_version', array('version_id' => $version_id)));
	iajax(0, '设置成功！', url('wxapp/front-download/front_download', array('version_id' => $version_id)));
}

if ('custom_convert_img' == $do) {
	$attchid = intval($_GPC['att_id']);
	$filename = miniapp_code_path_convert($attchid);
	iajax(0, $filename);
}

if ('set_appurl' == $do) {
	$appurl = safe_gpc_url($_GPC['appurl'], false);
	if (!starts_with($appurl, 'https')) {
		iajax(-1, '域名必须以https开头');
	}
	$update = table('account_wxapp')->where(['uniacid' => $_W['uniacid']])->fill(['appdomain' => $appurl])->save();
	iajax(0, '更新成功！');
}

if ('upload_verify_txt_file' == $do) {
	if (empty($_FILES['file']['name']) || 0 != $_FILES['file']['error']) {
		itoast('上传失败，错误码：' . $_FILES['file']['error']);
	}
	if ($_FILES['file']['type'] != 'text/plain') {
		itoast('文件类型错误');
	}
	$file = file_get_contents($_FILES['file']['tmp_name']);
	if (!preg_match('/^[A-Za-z0-9]+$/', $file)) {
		itoast('上传文件不合法,请重新上传！');
	}
	setting_save($file, $_FILES['file']['name']);
	uni_setting_save('verify_txt_file', $file);
	file_put_contents(IA_ROOT . '/' . $_FILES['file']['name'], $file);
	itoast('上传成功！', referer(), 'success');
}

if ('auth_domain_list' == $do) {
	if ($_W['account']->type == ACCOUNT_TYPE_APP_AUTH) {
		$webviewdomain = $_W['account']->setWebViewDomain(['action' => 'get']);
		if (is_error($webviewdomain)) {
			itoast($webviewdomain['message']);
		}
	}
	template('wxapp/domainset');
}

if ('auth_domain_post' == $do) {
	$action = safe_gpc_string($_GPC['action']);
	$action = in_array($action, ['add', 'delete']) ? $action : 'add';
	$webviewdomain = safe_gpc_string($_GPC['webviewdomain']);
	
	if (empty($webviewdomain) || substr($webviewdomain, 0, 8) !== "https://") {
		iajax(-1, '请输入正确的业务域名!');
	}
	$data = ['action' => $action, 'webviewdomain' => $webviewdomain];
	$result = $_W['account']->setWebViewDomain($data);
	if (is_error($result)) {
		iajax(-1, $result['message']);
	}
	cache_delete(cache_system_key('account_web_view_domain', array('uniacid' => $_W['uniacid'])));
	iajax(0, '操作成功!', referer());
}
if ('front_download' == $do) {
	permission_check_account_user('publish_front_download');
	$package_url = url('wxapp/front-download/getpackage', ['module_name' => key($version_info['modules']), 'version_id' => $version_id, '__session' => $_GPC['__session']], true);
	$appurl = !empty($_W['account']['appdomain']) ? $_W['account']['appdomain'] : ($_W['siteroot'] . 'app/index.php');
	$privatekey = [
		'key' => empty($_W['account']['upload_private_key']) ? '' : $_W['account']['upload_private_key'],
		'tool_id' => empty($_W['account']['tool_id']) ? '' : $_W['account']['tool_id']
	];
	$uptype = safe_gpc_string($_GPC['uptype']);
	if (!in_array($uptype, array('auto', 'normal'))) {
		$uptype = 'normal';
	}
	if (empty($version_info) || empty($version_info['modules'])) {
		$extend_buttons['status_console_button'] = [
			'url' => url('miniapp/manage/display', ['uniacid' => $_W['uniacid'], 'account_type' => $_W['account']['type']]),
			'class' => 'btn btn-default',
			'title' => '去配置',
		];
		itoast('请先分配应用后再发布！', '', 'expired', $extend_buttons);
	}
	$pay_status = STATUS_OFF;
	if (WXAPP_TYPE_SIGN == $_W['account']['type_sign']) {
		$verify_txt_file = !empty($_W['account']['setting']['verify_txt_file']) ? ('MP_verify_' . $_W['account']['setting']['verify_txt_file'] . '.txt') : '';
		$can_upload = (!empty($_W['account']['upload_private_key']) && $verify_txt_file) ? STATUS_ON : STATUS_OFF;
		if (!empty($version_info['last_modules'])) {
			$last_modules = current($version_info['last_modules']);
		}
		$need_upload = false;
		$module = [];
		if (!empty($version_info['modules'])) {
			foreach ($version_info['modules'] as $item) {
				$module = module_fetch($item['name']);
				$need_upload = !empty($last_modules) && ($module['version'] != $last_modules['version']);
			}
		}
		$pay_status = cloud_get_mini_privacy_upload();
	}
	if (!empty($version_info['version'])) {
		$user_version = explode('.', $version_info['version']);
		$user_version[count($user_version) - 1] += 1;
		$user_version = join('.', $user_version);
	}
	template('wxapp/version-front-download');
}

if ('platform_version_manage' == $do) {
	$platform_version_info = array('success' => array(), 'audit' => array(), 'develop' => array());
	$wxapp_register_version = table('wxapp_register_version')->getByUniacid($_W['uniacid']);
	foreach ($wxapp_register_version as $key => $value) {
		if (WXAPP_REGISTER_VERSION_STATUS_RELEASE == $value['status']) {
			$platform_version_info['success'][] = $value;
		} elseif (in_array($value['status'], array(WXAPP_REGISTER_VERSION_STATUS_CHECKING, WXAPP_REGISTER_VERSION_STATUS_CHECKFAIL, WXAPP_REGISTER_VERSION_STATUS_CHECKSUCCESS))) {
			$params = array(
				':uniacid' => $value['uniacid'],
				':version_id' => $value['version_id'],
			);
			$day_num = pdo_fetch('select count(id) day_num from ' . tablename('wxapp_undocodeaudit_log') . ' where TO_DAYS(from_unixtime(`revoke_time`)) = TO_DAYS(NOW()) and uniacid = :uniacid and version_id = :version_id;', $params);
			$month_num = pdo_fetch('select count(id) month_num from ' . tablename('wxapp_undocodeaudit_log') . ' where DATE_FORMAT(from_unixtime(`revoke_time`), "%Y%m")=DATE_FORMAT(CURDATE(), "%Y%m") and uniacid = :uniacid and version_id = :version_id;', $params);
			$value['day_num'] = empty($day_num) || $day_num['day_num'] < 1 ? 1 : 0;
			if (empty($month_num)) {
				$value['month_num'] = 10;
			} else {
				$value['month_num'] = $month_num['month_num'] >= 10 ? 0 : 10 - $month_num['month_num'];
			}
			$platform_version_info['audit'][] = $value;
		} elseif (WXAPP_REGISTER_VERSION_STATUS_DEVELOP == $value['status']) {
			$platform_version_info['develop'][] = $value;
		}
	}
	template('wxapp/version-front-download');
}

if ('upgrade_module' == $do) {
	$version = empty($_GPC['version']) ? '' : safe_gpc_string($_GPC['version']);
	$description = empty($_GPC['description']) ? '' : safe_gpc_html($_GPC['description']);
	$modules = table('wxapp_versions')
		->where(array('id' => $version_id))
		->getcolumn('modules');
	$modules = iunserializer($modules);
	if (!empty($modules)) {
		foreach ($modules as $name => $module) {
			$module_info = module_fetch($name);
			if (!empty($module_info['version'])) {
				$modules[$name]['version'] = $module_info['version'];
			}
		}
		$modules = iserializer($modules);
		table('wxapp_versions')
			->where(array('id' => $version_id))
			->fill(array(
				'modules' => $modules,
				'last_modules' => $modules,
				'version' => $version,
				'description' => $description,
				'upload_time' => TIMESTAMP,
			))
			->save();
		cache_delete(cache_system_key('miniapp_version', array('version_id' => $version_id)));
	}
	iajax(0, '更新模块信息成功');
}

if (in_array($do, array('rgapi_publish', 'rgapi_publish_status'))) {
	if (ACCOUNT_TYPE_APP_AUTH == $_W['account']['type']) {
		if (empty($_W['setting']['platform']['authstate'])) {
			iajax(-1, '开放平台未开启，无法上传');
		}
		if (empty($_W['setting']['platform']['bindappid']) || empty($_W['setting']['platform']['upload_private_key'])) {
			iajax(-1, '未设置开放平台绑定的开发小程序或代码上传秘钥');
		}
	}
	$headers = [
		'W7-KEY' => $_W['setting']['site']['key'],
		'W7-TOKEN' => md5($_W['setting']['site']['key'] . $_W['setting']['site']['token']),
	];
	$publish_data = [
		'appid' => ACCOUNT_TYPE_APP_AUTH == $_W['account']['type'] ? $_W['setting']['platform']['bindappid'] : $_W['account']['key']
	];
}
if ('rgapi_publish' == $do) {
	$publish_data['accountType'] = ACCOUNT_TYPE_ALIAPP_NORMAL == $_W['account']['type'] ? 5 : 2;
	$publish_data['privateKey'] = ACCOUNT_TYPE_APP_AUTH == $_W['account']['type'] ? $_W['setting']['platform']['upload_private_key'] : $_W['account']['upload_private_key'];
	
	$publish_data['privateKey'] = str_replace("\r\n", "", $publish_data['privateKey']);
	if (empty($publish_data['privateKey'])) {
		iajax(-1, '请先配置小程序上传秘钥！');
	}
	if (ACCOUNT_TYPE_ALIAPP_NORMAL == $_W['account']['type']) {
		$publish_data['toolId'] = $_W['account']['tool_id'];
		if (empty($publish_data['toolId'])) {
			iajax(-1, '请先配置小程序工具ID！');
		}
	}
	$module_name = key($version_info['modules']);
	$module_info = cloud_m_info($module_name);
	if (is_error($module_info) || empty($module_info['service_expiretime']) || TIMESTAMP > $module_info['service_expiretime']) {
		iajax(-1, '当前应用服务已到期，请联系管理员开通自助上传服务！');
	}
	$publish_data['url'] = url('wxapp/front-download/getpackage', ['module_name' => $module_name, 'version_id' => $version_id, '__session' => $_GPC['__session']], true);
	$publish_data['version'] = empty($_GPC['version']) ? '' : safe_gpc_string($_GPC['version']);
	if (WXAPP_TYPE_SIGN == $_W['account']['type_sign']) {
		$publish_data['desc'] = empty($_GPC['description']) ? '' : safe_gpc_string($_GPC['description']);
		$publish_data['usePlugin'] = empty($_GPC['usePlugin']) ? false : true;
		$publish_data['livePlugin'] = empty($_GPC['livePlugin']) ? false : true;
		$publish_data['usePrivateInfos'] = empty($_GPC['usePrivateInfo']) ? false : true;
		$publish_data['privacyWindows'] = empty($_GPC['privacyWindows']) ? false : true;
		$publish_data['setting']['es6'] = empty($_GPC['setting']['es6']) ? false : true;
		$publish_data['setting']['minify'] = empty($_GPC['setting']['minify']) ? false : true;
		$publish_data['setting']['minifyWXSS'] = empty($_GPC['setting']['minifyWXSS']) ? false : true;
		$publish_data['setting']['minifyWXML'] = empty($_GPC['setting']['minifyWXML']) ? false : true;
		$publish_data['setting']['minifyJS'] = empty($_GPC['setting']['minifyJS']) ? false : true;
		$publish_data['setting']['autoPrefixWXSS'] = empty($_GPC['setting']['autoPrefixWXSS']) ? false : true;
		$publish_data['setting']['codeProtect'] = empty($_GPC['setting']['codeProtect']) ? false : true;
	}

	$miniapp_push = ihttp_request(CLOUD_MINIAPP_DOMAIN_PRE . '/push', json_encode($publish_data), $headers);
	$miniapp_push = json_decode($miniapp_push['content'], true);
	if (200 != $miniapp_push['code']) {
		iajax(-1, $miniapp_push['message']);
	}
	iajax(0, 'success');
}

if ('rgapi_publish_status' == $do) {
	$miniapp_status = ihttp_request(CLOUD_MINIAPP_DOMAIN_PRE . '/status', $publish_data, $headers);
	$miniapp_status = json_decode($miniapp_status['content'], true);
	if (200 != $miniapp_status['code']) {
		iajax(-1, $miniapp_status['message']);
	}
	iajax(0, $miniapp_status['data']);
}

if ('publish_buy' == $do) {
	$data = [
		'body' => '微信小程序上传隐私指引',
		'detail' => '微信小程序上传隐私指引服务费',
		'type' => 'mini_privacy_upload',
	];
	$ticket = cloud_wxapp_get_transactions_ticket($data);
	if (is_error($ticket)) {
		iajax(-1, $ticket['message']);
	}
	iajax(0, $ticket);
}

if ('tominiprogram' == $do) {
	$tomini_lists = iunserializer($version_info['tominiprogram']);
	if (!is_array($tomini_lists)) {
		$tomini_lists = array();
		miniapp_version_update($version_id, array('tominiprogram' => iserializer(array())));
	}

	if (checksubmit()) {
		$appids = safe_gpc_array($_GPC['appid']);
		$app_names = safe_gpc_array($_GPC['app_name']);
		$is_add = intval($_GPC['is_add']);

		if (!is_array($appids) || !is_array($app_names)) {
			itoast('参数有误！', referer(), 'error');
		}
		$data = $is_add ? $tomini_lists : array();
		foreach ($appids as $k => $appid) {
			if (empty($appid) || empty($app_names[$k])) {
				continue;
			}
			$appid = safe_gpc_string($appid);
			$data[$appid] = array(
				'appid' => $appid,
				'app_name' => safe_gpc_string($app_names[$k])
			);
			if (count($data) >= 10) {
				break;
			}
		}
		miniapp_version_update($version_id, array('tominiprogram' => iserializer($data)));
		itoast('保存成功！', referer(), 'success');
	}
	template('wxapp/version-front-download');
}

if ('getpackage' == $do) {
	if (empty($version_id)) {
		itoast('参数错误！', '', '');
	}
	$account_wxapp_info = miniapp_fetch($version_info['uniacid'], $version_id);
	if (empty($account_wxapp_info)) {
		itoast('版本不存在！', referer(), 'error');
	}
	if (!is_dir(ATTACHMENT_ROOT . '/siteinfo')) {
		mkdir(ATTACHMENT_ROOT . '/siteinfo');
	}
	$module = array_shift($account_wxapp_info['version']['modules']);
	$dir_name = $module['name'] . '_' . $_W['account']['type_sign'];
	$zip = new ZipArchive();
	if (!is_file(IA_ROOT . '/addons/' . $module['name'] . '/' . $dir_name . '/' . $module['version'])) {
		rmdirs(IA_ROOT . '/addons/' . $module['name'] . '/' . $dir_name);
		$request_cloud_data = array(
			'module' => $module,
			'support' => $_W['account']['type_sign'],
			'filename' => 'package.zip'
		);
		$cloud_package = cloud_miniapp_get_package($request_cloud_data);
		if (is_error($cloud_package)) {
			itoast($cloud_package['message']);
		}
		$package = $module['name'] . '_' . $_W['account']['type_sign'] . '_' . $_W['uniacid'] . time() . '.zip';
		$download = file_put_contents(ATTACHMENT_ROOT . '/siteinfo/' . $package, file_get_contents($cloud_package['url']));
		if (empty($download)) {
			itoast('文件下载失败！');
		}
		@chmod(ATTACHMENT_ROOT . '/siteinfo/' . $package, 0755);
		if ($zip->open(ATTACHMENT_ROOT . '/siteinfo/' . $package)) {
			$unzip = $zip->extractTo(ATTACHMENT_ROOT . '/siteinfo');
			$zip->close();
			if (empty($unzip)) {
				itoast('文件解压失败！');
			}
		} else {
			itoast('文件解压失败！');
		}
		rename(ATTACHMENT_ROOT . '/siteinfo/' . $module['name'], ATTACHMENT_ROOT . '/siteinfo/' . $dir_name);
		file_copy(ATTACHMENT_ROOT . '/siteinfo/' . $dir_name, IA_ROOT . '/addons/' . $module['name'] . '/' . $dir_name, array());
		file_put_contents(IA_ROOT . '/addons/' . $module['name'] . '/' . $dir_name . '/' . $module['version'], '');
		@unlink(ATTACHMENT_ROOT . '/siteinfo/' . $package);
		rmdirs(ATTACHMENT_ROOT . '/siteinfo/' . $dir_name);
	}

	$module_root = IA_ROOT . '/addons/' . $module['name'] . '/';
	$uniacid_zip_name = $module['name'] . '_' . $_W['account']['type_sign'] . '_' . $_W['uniacid'] . md5(time()) . '.zip';
	if ($_W['account']['type_sign'] == 'wxapp') {
		$app_json = array();
		$tomini_lists = iunserializer($version_info['tominiprogram']);
		if (!empty($tomini_lists) && file_exists($module_root . $dir_name . '/app.json')) {
			$app_json = json_decode(file_get_contents($module_root . $dir_name . '/app.json'));
			$app_json->embeddedAppIdList = array_keys($version_info['tominiprogram']);
		}
		
		$app_json_data = json_decode(file_get_contents($module_root . $dir_name . '/app.json'), true);
		if (!empty($app_json_data['requiredPrivateInfos'])) {
			if (file_exists($module_root . $dir_name . '/ext.json')) {
				$ext_json_data = json_decode(file_get_contents($module_root . $dir_name . '/ext.json'), true);
				$ext_json_data['requiredPrivateInfos'] = $app_json_data['requiredPrivateInfos'];
			} else {
				$ext_json_data = [
					'extEnable' => true,
					'extAppid' => $_W['account']['key'],
					'permission' => $app_json_data['permission'],
					'requiredPrivateInfos' => $app_json_data['requiredPrivateInfos']
				];
			}
			$ext_json_data = json_encode($ext_json_data);
			$tmp_ext_json_file = 'siteinfo/ext_' . $_W['uniacid'] . '.json';
			file_write($tmp_ext_json_file, $ext_json_data);
			file_put_contents($module_root . $dir_name . '/ext.json', $ext_json_data);
		}
	}
	if ($zip->open($module_root . $uniacid_zip_name, ZipArchive::CREATE) === true) {
		addFileToZip($module_root . $dir_name, $zip, $module_root . $dir_name . '/');
		$zip->close();
	}
	$copy_result = copy($module_root . $uniacid_zip_name, ATTACHMENT_ROOT . '/siteinfo/' . $uniacid_zip_name);
	if (!$copy_result) {
		itoast('小程序前端报预处理打包失败，请将权限设置成755后再试！');
	} else {
		@unlink($module_root . $uniacid_zip_name);
	}
	$siteurl = $_W['siteroot'] . 'app/index.php';
	if (!empty($account_wxapp_info['appdomain'])) {
		$siteurl = $account_wxapp_info['appdomain'];
	}
	if ($_W['account']['type_sign'] == 'wxapp') {
		$siteinfo_content = <<<EOF
var siteinfo = {
"name": "{$module['name']}",
"module_version": "{$module['version']}",
"uniacid": "{$_W['uniacid']}",
"acid": "{$_W['uniacid']}",
"multiid": "0",
"version": "{$version_info['version']}",
"siteroot": "$siteurl",
"method_design": "3"
};
module.exports = siteinfo;
EOF;
		$tmp_siteinfo_file = 'siteinfo/siteinfo_' . $_W['uniacid'] . '.js';
		file_write($tmp_siteinfo_file, $siteinfo_content);
		$tmp_app_json_file = '';
		if (!empty($app_json)) {
			$tmp_app_json_file = 'siteinfo/app_' . $_W['uniacid'] . '.json';
			file_write($tmp_app_json_file, json_encode($app_json));
		}
		if ($zip->open(ATTACHMENT_ROOT . '/siteinfo/' . $uniacid_zip_name) === true) {
			$zip->deleteName($module['version']);
			$zip->deleteName('siteinfo.js');
			$zip->addFile(ATTACHMENT_ROOT . '/' . $tmp_siteinfo_file, 'siteinfo.js');
			if (!empty($tmp_app_json_file)) {
				$zip->deleteName('app.json');
				$zip->addFile(ATTACHMENT_ROOT . '/' . $tmp_app_json_file, 'app.json');
			}
			if (!empty($tmp_ext_json_file)) {
				$zip->deleteName('ext.json');
				$zip->addFile(ATTACHMENT_ROOT . '/' . $tmp_ext_json_file, 'ext.json');
			}
			$zip->close();
		}
		@unlink(ATTACHMENT_ROOT . '/' . $tmp_siteinfo_file);
		if (!empty($tmp_app_json_file)) {
			@unlink(ATTACHMENT_ROOT . '/' . $tmp_app_json_file);
		}
		if (!empty($tmp_ext_json_file)) {
			@unlink(ATTACHMENT_ROOT . '/' . $tmp_ext_json_file);
		}
	} else {
		if ($zip->open(ATTACHMENT_ROOT . '/siteinfo/' . $uniacid_zip_name) === true) {
			$zip->deleteName($module['version']);
			$zip->close();
		}
	}

	header('Content-Type: application/octet-stream');
	header('Content-Disposition:attachment;filename=' . $uniacid_zip_name);
	$fp = fopen(ATTACHMENT_ROOT . '/siteinfo/' . $uniacid_zip_name, 'r+');
	$buffer = 1024;
	while (!feof($fp)) {
		$file_data = fread($fp, $buffer);
		echo $file_data;
	}
	fclose($fp);
}

if ('upload_private_key' == $do) {
	$upload_private_key = safe_gpc_string($_GPC['upload_private_key']);
	if (empty($upload_private_key)) {
		iajax(1, '小程序上传密钥不能为空！');
	}
	if (WXAPP_TYPE_SIGN == $_W['account']['type_sign']) {
		pdo_update('account_wxapp', array('upload_private_key' => $upload_private_key), array('uniacid' => $_W['uniacid']));
	} else {
		$tool_id = safe_gpc_string($_GPC['tool_id']);
		if (empty($tool_id)) {
			iajax(1, '工具Id不能为空！');
		}
		if (32 != istrlen($tool_id)) {
			iajax(1, '工具Id必须是32个字符！');
		}
		pdo_update('account_aliapp', array('upload_private_key' => $upload_private_key, 'tool_id' => $tool_id), array('uniacid' => $_W['uniacid']));
	}
	cache_delete(cache_system_key('uniaccount', array('uniacid' => $_W['uniacid'])));
	iajax(0, '上传配置成功！', url('wxapp/front-download/front_download', array('version_id' => $version_id)));
}

function addFileToZip($path, $zip, $root_path)
{
	$handler = opendir($path);
	while (($filename = readdir($handler)) !== false) {
		if ($filename != "." && $filename != "..") {
			if (is_dir($path . "/" . $filename)) {
				addFileToZip($path . "/" . $filename, $zip, $root_path);
			} else {
				$zip->addFile($path . "/" . $filename, substr($path . "/" . $filename, strlen($root_path)));
			}
		}
	}
	@closedir($handler);
	return true;
}
